#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.cycle;

import lombok.RequiredArgsConstructor;
import ${package}.config.rabbit.RabbitMqHandle;
import ${package}.config.rabbit.property.RabbitMqProperties;
import ${package}.utils.ConsistenceHashUtils;
import ${package}.utils.ManualRegistBeanUtil;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enable", havingValue = "true")
public class TopicLifeCycle implements SmartLifecycle {
    private final RabbitMqProperties rabbitMqProperties;
    private final AmqpAdmin amqpAdmin;
    private final ApplicationContext applicationContext;

    @Override
    public void start() {
        //初始化topic
        RabbitMqHandle rabbitMqHandle = new RabbitMqHandle(amqpAdmin);
        rabbitMqHandle.initRabbitMqConfig(rabbitMqProperties);
        Map<String, List<String>> queueMap = rabbitMqHandle.getQueueNameByExchange(rabbitMqProperties);
        Map<String, ConsistenceHashUtils> getConsistenceHashMap = rabbitMqHandle.getServerName(rabbitMqProperties);
        ManualRegistBeanUtil.registerBean((ConfigurableApplicationContext) applicationContext, rabbitMqHandle,"rabbitMqHandle");
        ManualRegistBeanUtil.registerBean((ConfigurableApplicationContext) applicationContext, queueMap,"queueMap");
        ManualRegistBeanUtil.registerBean((ConfigurableApplicationContext) applicationContext, getConsistenceHashMap,"getConsistenceHashMap");
    }

    @Override
    public void stop() {

    }

    @Override
    public boolean isRunning() {
        return false;
    }

    @Override
    public int getPhase() {
        return -1;
    }
}
